<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title">
        <h2>{{ isLogin ? '登录' : '注册' }}</h2>
      </div>
      <el-form :model="form" :rules="rules" ref="loginForm" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item v-if="!isLogin" label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-mobile-phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading" class="submit-btn">{{ isLogin ? '登录' : '注册' }}</el-button>
          <el-button @click="resetForm" class="reset-btn">重置</el-button>
        </el-form-item>
        <div class="form-footer">
          <span v-if="isLogin">
            还没有账号？
            <a href="javascript:;" @click="toggleForm">立即注册</a>
          </span>
          <span v-else>
            已有账号？
            <a href="javascript:;" @click="toggleForm">立即登录</a>
          </span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'LoginView',
  data() {
    // 验证两次密码是否一致
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      isLogin: true,
      loading: false,
      form: {
        username: 'admin',
        password: '123456',
        confirmPassword: '123456',
        phone: '12345678901',
        email: '12345678901@163.com'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['login', 'register']),
    submitForm() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            if (this.isLogin) {
              // 登录
              await this.login({
                username: this.form.username,
                password: this.form.password
              })
              this.$message.success('登录成功')
            } else {
              // 注册
              await this.register({
                username: this.form.username,
                password: this.form.password,
                phone: this.form.phone,
                email: this.form.email
              })
              this.$message.success('注册成功')
            }
            // 跳转到首页或重定向页面
            const redirect = this.$route.query.redirect || '/'
            this.$router.push(redirect)
          } catch (error) {
            this.$message.error(error || (this.isLogin ? '登录失败' : '注册失败'))
          } finally {
            this.loading = false
          }
        }
      })
    },
    resetForm() {
      this.$refs.loginForm.resetFields()
    },
    toggleForm() {
      this.isLogin = !this.isLogin
      this.resetForm()
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
}

.login-card {
  width: 450px;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
  transition: all 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
}

.title {
  text-align: center;
  margin-bottom: 30px;
}

.title h2 {
  color: #3F5D62;
  font-weight: 600;
  font-size: 28px;
  margin: 0;
}

.submit-btn {
  width: 48%;
  background-color: #3F5D62 !important;
  border-color: #3F5D62 !important;
  font-weight: 500;
  letter-spacing: 1px;
  padding: 12px 0;
  border-radius: 4px;
  transition: all 0.3s;
}

.submit-btn:hover, .submit-btn:focus {
  background-color: #4a6d73 !important;
  border-color: #4a6d73 !important;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(63, 93, 98, 0.3);
}

.reset-btn {
  width: 48%;
  border-color: #d0d7de;
  color: #606266;
  padding: 12px 0;
  border-radius: 4px;
  transition: all 0.3s;
}

.reset-btn:hover {
  color: #3F5D62;
  border-color: #3F5D62;
  background-color: rgba(63, 93, 98, 0.05);
}

.form-footer {
  text-align: center;
  margin-top: 25px;
  color: #606266;
  font-size: 14px;
}

.form-footer a {
  color: #3F5D62;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s;
}

.form-footer a:hover {
  color: #4a6d73;
  text-decoration: underline;
}

/* 输入框样式 */
.el-input__inner {
  border-radius: 4px;
  padding-left: 40px;
  height: 42px;
  transition: all 0.3s;
}

.el-input__inner:focus {
  border-color: #3F5D62;
}

.el-input__prefix {
  left: 10px;
  color: #909399;
}

.el-form-item__label {
  color: #606266;
  font-weight: 500;
}
</style> 